package com.xcity.db.entity;

import java.util.Date;
import java.util.List;

import com.xcity.game.account.AccountChannel;
import com.xcity.util.IdUtils;

import gnu.trove.list.TLongList;
import gnu.trove.list.array.TLongArrayList;
import naga.x.util.CommonUtils;
import naga.x.util.StringUtils;

public class User {

	private long id;
	private String name;
	private String nickname;
	private String password;
	private String salt;
	private Date createTime;
	private Date lastLoginTime;
	private String lastLoginIp;
	private Date lastLogoutTime;
	private String phone;
	private AccountChannel channel; // 渠道: 1、官方 2、微信 3、白鹭 4、电信
	private String channelUid;
	
	protected String token;
	protected TLongList playerIds = new TLongArrayList(3);
	
	public static final int SALT_DEFAULT_LEN = 8;
	public static final String DEAFULT_PASSWORD = "_XCITY_";
	
	public User() {}
	
	public User(String nickname, String nativePassword, String lastLoginIp, String phone) {
//		this(IdUtils.nextAccountId(), name, name, nativePassword, CommonUtils.randomText(SALT_DEFAULT_LEN), lastLoginIp, phone);
		this.id = IdUtils.nextAccountId();
		this.name = nickname + ':' + id;
		this.nickname = nickname;
		this.salt = CommonUtils.randomText(SALT_DEFAULT_LEN);
		this.password = CommonUtils.encryptToHexString("MD5", StringUtils.isEmpty(salt) ? nativePassword : (nativePassword + salt));
		this.createTime = new Date();
		this.lastLoginIp = lastLoginIp;
		this.phone = phone;
	}
	
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getSalt() {
		return salt;
	}

	public void setSalt(String salt) {
		this.salt = salt;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public String getLastLoginIp() {
		return lastLoginIp;
	}

	public void setLastLoginIp(String lastLoginIp) {
		this.lastLoginIp = lastLoginIp;
	}

	public Date getLastLoginTime() {
		return lastLoginTime;
	}

	public void setLastLoginTime(Date lastLoginTime) {
		this.lastLoginTime = lastLoginTime;
	}

	public Date getLastLogoutTime() {
		return lastLogoutTime;
	}

	public void setLastLogoutTime(Date lastLogoutTime) {
		this.lastLogoutTime = lastLogoutTime;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public AccountChannel getChannel() {
		return channel;
	}

	public void setChannel(AccountChannel channel) {
		this.channel = channel;
	}

	public String getChannelUid() {
		return channelUid;
	}

	public void setChannelUid(String channelUid) {
		this.channelUid = channelUid;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public TLongList getPlayerIds() {
		return playerIds;
	}

	public void setPlayerIds(List<Long> playerIds) {
		if (playerIds != null && playerIds.size() > 0) {
			this.playerIds.clear();
			this.playerIds.addAll(playerIds);
		}
	}
	
	public void addPlayer(long pid) {
		playerIds.add(pid);
	}
	
	public long getDefaultPlayerId() {
		TLongList arr = playerIds;
		return arr != null && arr.size() > 0 ? arr.get(0) : 0;
	}
	
}
